home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / SETENVAR.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  3KB  |  129 lines

  1. /* +++Date last modified: 28-Sep-1996 */
  2.  
  3. /*
  4. **  SETENVAR.C - Program which sets the DOS master environment upon exit
  5. **
  6. **  Original Copyright 1988-1991 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <dos.h>
  19. #include "sniptype.h"
  20. #include "pchwio.h"
  21. #include "errors.h"                       /* For cant()     */
  22.  
  23. static unsigned head, tail, start, end;
  24. static int idx = 0;
  25. static unsigned keystack[16][2];
  26.  
  27. /*
  28. **  ungetkey()
  29. **
  30. **  Stuffs characters into the keyboard buffer.
  31. **
  32. **  Parameters: 1 - Extended character to stuff
  33. **
  34. **  Returns: Success_ or EOF
  35. **
  36. **  Note: This function assumes that the keyboard buffer is in
  37. **        the normal (for IBM) location of 40:1E.
  38. **
  39. */
  40.  
  41. int ungetkey(unsigned key)
  42. {
  43.       int count;
  44.  
  45.       head  = peekw(0x40, 0x1a);
  46.       tail  = peekw(0x40, 0x1c);
  47.       start = peekw(0x40, 0x80);
  48.       end   = peekw(0x40, 0x82);
  49.  
  50.       count = tail - head;
  51.       if (0 > count)
  52.             count += (16 * sizeof(unsigned));
  53.       count >>= 1;
  54.  
  55.       if (15 > count)
  56.       {
  57.             disable();
  58.             keystack[idx][0] = peekw(0x40, tail);
  59.             keystack[idx][1] = tail;
  60.             poke(0x40, tail, key);
  61.             tail += sizeof(unsigned);
  62.             if (end <= tail)
  63.                   tail = start;
  64.             poke(0x40, 0x1c, tail);
  65.             enable();
  66.             return key;
  67.       }
  68.       return EOF;
  69. }
  70.  
  71. /*
  72. **  KB_stuff()
  73. **
  74. **  Stuffs strings into the keyboard buffer.
  75. **
  76. **  Parameters: 1 - String to stuff
  77. **
  78. **  Returns: Success_ if successful
  79. **           Error_   in case of error, plus keyboard buffer is
  80. **                    restored
  81. **
  82. **  Note: This function assumes that the keyboard buffer is in
  83. **        the normal (for IBM) location of 40:1E.
  84. */
  85.  
  86. int KB_stuff(char *str)
  87. {
  88.       int ercode = Success_;
  89.  
  90.       idx = 0;
  91.       while (*str)
  92.       {
  93.             if (EOF == ungetkey((unsigned)(*str++)))
  94.             {
  95.                   disable();
  96.                   while (0 <= --idx)
  97.                   {
  98.                         tail = keystack[idx][1];
  99.                         poke(0x40, tail, keystack[idx][0]);
  100.                   }
  101.                   poke(0x40, 0x1c, tail);
  102.                   enable();
  103.                   ercode = Error_;
  104.                   break;
  105.             }
  106.             else  ++idx;
  107.       }
  108.       idx = 0;
  109.       return ercode;
  110. }
  111.  
  112. main(int argc, char *argv[])
  113. {
  114.       FILE *bfile;
  115.  
  116.       if (3 > argc)
  117.       {
  118.             puts("\aUsage: SETENVAR envar datum");
  119.             abort();
  120.       }
  121.       bfile = cant("$TMP$.BAT", "w");
  122.       fprintf(bfile, "SET %s=%s\ndel $tmp$.bat\x1a", argv[1], argv[2]);
  123.       fclose(bfile);
  124.       while (kbhit())
  125.             getch();
  126.       KB_stuff("$tmp$\r");
  127.       return 0;
  128. }
  129.